define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form, JsTree) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'data/admin.json',
                    add_url: 'admin-edit.html',
                    edit_url: 'admin-edit.html',
                    del_url: 'data/del.json',
                    multi_url: 'data/multi.json',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        { field: 'id', title: '序号' },
                        { field: 'area', title: '归属地区' },
                        { field: 'nickname', title: "公司名称" },
                        {field: 'username', title: "姓名"},
                        {field: 'phone', title: "手机"},
                        
                        {field: 'status', title: __("Status"), formatter: Table.api.formatter.status},
                        {field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: function (value, row, index) {
                                if(row.id == 1){
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index, table);
                            }}
                    ]
                ],
                //禁用默认搜索
                search: false,
            //启用普通表单搜索
            commonSearch: true,
            //可以控制是否默认显示搜索单表,false则隐藏,默认为false
            searchFormVisible: false,
            detailView: false,
            showExport: false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //加载jstree
            $('#jstree21').jstree({
                'core': {
                    'check_callback': true
                },
                'plugins': [ 'types', 'dnd'],
                'types': {
                    'default': {
                        'icon': 'fa fa-folder'
                    },
                    'html': {
                        'icon': 'fa fa-file-code-o'
                    },
                    'svg': {
                        'icon': 'fa fa-file-picture-o'
                    },
                    'css': {
                        'icon': 'fa fa-file-code-o'
                    },
                    'img': {
                        'icon': 'fa fa-file-image-o'
                    },
                    'son': {
                        'icon': 'fa fa-file-text-o'
                    }

                }
            });
        },
        add: function () {
            //加载jstree
            $('#jstree2').jstree({
                'core': {
                    'check_callback': true
                },
                'plugins': ['checkbox', 'types', 'dnd'],
                'types': {
                    'default': {
                        'icon': 'fa fa-folder'
                    },
                    'html': {
                        'icon': 'fa fa-file-code-o'
                    },
                    'svg': {
                        'icon': 'fa fa-file-picture-o'
                    },
                    'css': {
                        'icon': 'fa fa-file-code-o'
                    },
                    'img': {
                        'icon': 'fa fa-file-image-o'
                    },
                    'son': {
                        'icon': 'fa fa-file-text-o'
                    }

                }
            });
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            //加载jstree
            $('#jstree2').jstree({
                'core': {
                    'check_callback': true
                },
                'plugins': ['checkbox', 'types', 'dnd'],
                'types': {
                    'default': {
                        'icon': 'fa fa-folder'
                    },
                    'html': {
                        'icon': 'fa fa-file-code-o'
                    },
                    'svg': {
                        'icon': 'fa fa-file-picture-o'
                    },
                    'css': {
                        'icon': 'fa fa-file-code-o'
                    },
                    'img': {
                        'icon': 'fa fa-file-image-o'
                    },
                    'son': {
                        'icon': 'fa fa-file-text-o'
                    }

                }
            });

            $(document).on("click", ".fieldlist .append", function () {
                var rel = parseInt($(this).closest("dl").attr("rel")) + 1;
                var name = $(this).closest("dl").data("name");
                $(this).closest("dl").attr("rel", rel);
                var text = '"<dd class="form-inline" style="margin-left:12px;"> <input type="text" name="row[languages][value][backend]" class="form-control" placeholder="姓名" value="" size="40" /><input type="text" name="row[languages][value][backend]" class="form-control" placeholder="电话" value="" style="width:120px;" size="40" /><input type="password" class="form-control" id="password" name="row[password]" placeholder="密码" value="" style="width:100px;" data-rule="password" /><select id="c-type" class="form-control" name="row[type]"><option value="0">启用</option><option value="1">禁用</option></select><span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span></dd>"';
                $('<dd class="form-inline" style="margin-left:12px;"> <input type="text" name="row[languages][value][backend]" class="form-control" placeholder="姓名" value="" size="40" /> <input type="text" name="row[languages][value][backend]" class="form-control" placeholder="电话" value="" style="width:120px;" size="40" /> <input type="password" class="form-control" id="password" name="row[password]" placeholder="密码" value="" style="width:100px;" data-rule="password" /> <select id="c-type" class="form-control" name="row[type]"><option value="0">启用</option><option value="1">禁用</option></select> <span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span></dd>').insertBefore($(this).parent());
//$('<dd class="form-inline" style="margin-left:12px;"> <input type="text" name="' + name + '[value][' + rel + ']" class="form-control" value="" size="40" /> <span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> <span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span></dd>').insertBefore($(this).parent());
            });
            $(document).on("click", ".fieldlist dd .btn-remove", function () {
                $(this).parent().remove();
            });
            //拖拽排序
            require(['dragsort'], function () {
                //绑定拖动排序
                $("dl.fieldlist").dragsort({
                    itemSelector: 'dd',
                    dragSelector: ".btn-dragsort",
                    dragEnd: function () {

                    },
                    placeHolderTemplate: "<dd></dd>"
                });
            });

            Form.api.bindevent($("form[role=form]"));
            
            
        }
    };
    return Controller;
});